package com.yc.mvplib.Basis;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yc.mvplib.FastConfig;
import com.yc.mvplib.inter.IBasisView;
import com.yc.mvplib.manager.LoggerManager;

import com.yc.mvplib.utlis.FastUtil;
import com.yc.mvplib.utlis.StatusBarUtil;
import com.yc.mvplib.utlis.ToastUtil;
import com.yc.mvplib.utlis.UIHandler;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

/**
 * Created: AriesHoo on 2017/7/19 16:11
 * Function:所有Fragment的基类
 * Desc:
 */
public abstract class BasisFragment <P extends BasisPresenter, M extends BasisModel> extends SwipeBackFragment implements IBasisView {

    protected Activity mContext;
    protected View mContentView;
    protected boolean mIsFirstShow;
    protected boolean mIsViewLoaded;
    protected Unbinder mUnBinder;
    public P mPresenter;
    public M mModel;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (Activity) context;
        mIsFirstShow = true;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        beforeSetContentView();
        if (null==mContentView)
        mContentView = inflater.inflate(getContentLayout(), container, false);
        return attachToSwipeBack(mContentView);
    }

    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        //设置状态栏透明
        setStatusBarColor();
        mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mUnBinder = ButterKnife.bind(this, mContentView);
        mIsViewLoaded = true;
        EventBus.getDefault().register(this);
        mPresenter = FastUtil.getT(this, 0);
        mModel = FastUtil.getT(this, 1);
        beforeInitView();
        if (this instanceof BasisView) mPresenter.attachVM(this, mModel);
        initView(savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
    }
    public void setStatusBarColor() {
            StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), null);
    }
    @Override
    public int getContentBackground() {
        return FastConfig.getInstance(getContext()).getContentViewBackgroundResource();
    }

    @Override
    public void beforeSetContentView() {
        LoggerManager.i("====Fragment--beforeSetContentView====" );
    }

    @Override
    public void beforeInitView() {
        LoggerManager.i("====Fragment--beforeInitView====" );
        if (getContentBackground() > 0) {
            mContentView.setBackgroundResource(getContentBackground());
        }
        setSwipeBackEnable(false);
    }

    @Override
    public void loadData() {
        LoggerManager.i("====Fragment--开始数据加载====" );
    }
    public void onResume() {
        LoggerManager.i("====Fragment--onResume====" );
        super.onResume();

    }

    public void onPause() {
        LoggerManager.i("====Fragment--onPause====" );
        super.onPause();

    }

    public void showToast(String msg) {
        ToastUtil.showShort(this.getContext(),msg);
    }
    @Override
    public void onDestroyView() {
        LoggerManager.i("====Fragment--onDestroyView====" );
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        LoggerManager.i("====Fragment--onDestroy====" );
        EventBus.getDefault().unregister(this);
        if (mPresenter != null) mPresenter.detachVM();
        super.onDestroy();
    }

    /**
     * 不在viewpager中Fragment懒加载
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        onVisibleChanged(!hidden);
    }

    /**
     * 在viewpager中的Fragment懒加载
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        onVisibleChanged(isVisibleToUser);
    }

    /**
     * 用户可见变化回调
     *
     * @param isVisibleToUser
     */
    protected void onVisibleChanged(final boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (!mIsViewLoaded) {//确保视图加载及视图绑定完成避免刷新UI抛出异常
                UIHandler.get().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onVisibleChanged(isVisibleToUser);
                    }
                },10);

            } else {
                lazyLoad();
            }
        }
    }

    private void lazyLoad() {
        if (mIsFirstShow && mIsViewLoaded) {
            mIsFirstShow = false;
            loadData();
        }
    }
    @Override
    public Context getContext() {
        return mContext;
    }
}
